package br.com.pizzaria.util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.pizzaria.model.Ingrediente;
import br.com.pizzaria.model.dao.IngredienteDAO;

@FacesConverter(value = "ingredienteConverter")
public class IngredienteConverter implements Converter {

	private Ingrediente ingrediente = new Ingrediente();

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		if (value != null && !value.equals("")) {
			IngredienteDAO dao = new IngredienteDAO();
			ingrediente.setIdIngrediente(Integer.valueOf(value));
			return dao.consultar(ingrediente);
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
		if (value != null && !value.equals("")) {
			Ingrediente ingrediente = (Ingrediente) value;
			return ingrediente.getIdIngrediente() + "";
		}
		return null;
	}

}
